{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Number of Arithmetic Triplets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #two-pointers #enumeration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #双指针 #枚举"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: arithmeticTriplets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #算术三元组的数目"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始、<strong>严格递增</strong> 的整数数组 <code>nums</code> 和一个正整数 <code>diff</code> 。如果满足下述全部条件，则三元组 <code>(i, j, k)</code> 就是一个 <strong>算术三元组</strong> ：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>i &lt; j &lt; k</code> ，</li>\n",
    "\t<li><code>nums[j] - nums[i] == diff</code> 且</li>\n",
    "\t<li><code>nums[k] - nums[j] == diff</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>返回不同 <strong>算术三元组</strong> 的数目<em>。</em></p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [0,1,4,6,7,10], diff = 3\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>\n",
    "(1, 2, 4) 是算术三元组：7 - 4 == 3 且 4 - 1 == 3 。\n",
    "(2, 4, 5) 是算术三元组：10 - 7 == 3 且 7 - 4 == 3 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [4,5,6,7,8,9], diff = 2\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>\n",
    "(0, 2, 4) 是算术三元组：8 - 6 == 2 且 6 - 4 == 2 。\n",
    "(1, 3, 5) 是算术三元组：9 - 7 == 2 且 7 - 5 == 2 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>3 &lt;= nums.length &lt;= 200</code></li>\n",
    "\t<li><code>0 &lt;= nums[i] &lt;= 200</code></li>\n",
    "\t<li><code>1 &lt;= diff &lt;= 50</code></li>\n",
    "\t<li><code>nums</code> <strong>严格</strong> 递增</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [number-of-arithmetic-triplets](https://leetcode.cn/problems/number-of-arithmetic-triplets/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [number-of-arithmetic-triplets](https://leetcode.cn/problems/number-of-arithmetic-triplets/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[0,1,4,6,7,10]\\n3', '[4,5,6,7,8,9]\\n2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        l=len(nums)\n",
    "        res=0\n",
    "        for i in range(0,l-2):\n",
    "            for j in range(i+1,l-1):\n",
    "                if nums[j]>nums[i]+diff:\n",
    "                    break\n",
    "                elif nums[j]==nums[i]+diff:\n",
    "                    for k in range(j + 1, l):\n",
    "                        if nums[k] > nums[j] + diff:\n",
    "                            break\n",
    "                        if nums[k] - nums[j] == diff:\n",
    "                            res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "\n",
    "        ans, i, j = 0, 0, 1\n",
    "\n",
    "        for x in nums:  # x = nums[k]\n",
    "\n",
    "            while nums[j] + diff < x:\n",
    "\n",
    "                j += 1\n",
    "\n",
    "            if nums[j] + diff > x:\n",
    "\n",
    "                continue\n",
    "\n",
    "            while nums[i] + diff * 2 < x:\n",
    "\n",
    "                i += 1\n",
    "\n",
    "            if nums[i] + diff * 2 == x:\n",
    "\n",
    "                ans += 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        ans = 0\n",
    "        for num in nums:\n",
    "            if (num + diff) in nums and (num + 2 * diff) in nums:\n",
    "                ans +=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i+1, len(nums)):\n",
    "                for k in range(j+1, len(nums)):\n",
    "                    if nums[j] - nums[i] == diff and nums[k] - nums[j] == diff:\n",
    "                        res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        return sum(b - a == diff and c - b == diff for a, b, c in combinations(nums, 3))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(0, len(nums)-2):\n",
    "            for j in range(i+1, len(nums)-1):\n",
    "                for k in range(j+1, len(nums)):\n",
    "                    if nums[j] - nums[i] == nums[k] - nums[j] == diff:\n",
    "                        res += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        i = j = 0\n",
    "        res = 0\n",
    "        while i < len(nums) - 2:\n",
    "            while j < len(nums) - 1:\n",
    "                if nums[j] - nums[i] == diff:\n",
    "                    for k in range(j,len(nums)):\n",
    "                        if nums[k] - nums[j] == diff:\n",
    "                            res += 1\n",
    "                j += 1\n",
    "            j = 0\n",
    "            i += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        dic = {}\n",
    "        ans = 0\n",
    "        for i in nums:\n",
    "            if i not in dic:\n",
    "                dic[i] = 1\n",
    "        for j in nums:\n",
    "            if j + diff in dic and j + 2 * diff in dic:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        num_map = {}\n",
    "        ans = 0\n",
    "        for i in nums:\n",
    "            if i - diff in num_map:\n",
    "                num_map[i] = num_map[i - diff] + 1\n",
    "            else:\n",
    "                num_map[i] = 1\n",
    "            if num_map[i] >= 3:\n",
    "                ans += 1\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(len(nums) - 2):\n",
    "            if nums[i] + diff in nums and nums[i] + diff * 2 in nums:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        vs = set()\n",
    "        c = 0\n",
    "        for n in nums:\n",
    "            if n - diff in vs and n - 2*diff in vs:\n",
    "                c += 1\n",
    "            vs.add(n)\n",
    "        return c\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        dic = {}\n",
    "        ans = 0\n",
    "        for i in nums:\n",
    "            if i not in dic:\n",
    "                dic[i] = 1\n",
    "        for j in nums:\n",
    "            if j + diff in dic and j + 2 * diff in dic:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        res=0\n",
    "        i=0\n",
    "        while i<len(nums)-2:\n",
    "            tmp=nums[i]+diff\n",
    "            tmp_1=nums[i]+2*diff\n",
    "            if tmp in nums and tmp_1 in nums:\n",
    "                res+=1\n",
    "            i+=1\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        c=0\n",
    "        for i in range (len(nums)):\n",
    "            for j in range(i,len(nums)):\n",
    "                for  k in range (j,len(nums)):\n",
    "                    if i<j<k and nums[j] - nums[i] == diff and nums[k] - nums[j] == diff:\n",
    "                        c+=1\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        ans = 0\n",
    "        n = len(nums)\n",
    "        for j in range(1, n-1):\n",
    "            pre= 0\n",
    "            for i in range(j):\n",
    "                pre += int(nums[j]-nums[i] == diff)\n",
    "            for k in range(j+1, n):\n",
    "                ans += pre * int(nums[k]-nums[j] == diff)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        sum0=0\n",
    "        for i in range(len(nums)-2):\n",
    "            if nums[i]+diff in nums:\n",
    "                if nums[i]+2*diff in nums:\n",
    "                    sum0+=1\n",
    "        return sum0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        out=0\n",
    "        for i in nums:\n",
    "            if i+diff in nums and i-diff in nums:\n",
    "                out+=1\n",
    "        return out\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        return sum(num+diff in nums and num+diff*2 in nums for num in nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        n=len(nums)\n",
    "        a=0\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                for k in range (j+1,n):\n",
    "                    if nums[j]-nums[i]==diff and nums[k]-nums[j]==diff:\n",
    "                        a+=1\n",
    "                    else:\n",
    "                        continue\n",
    "        return a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        dic = {}\n",
    "        ans = 0\n",
    "        for i in nums:\n",
    "            if i not in dic:\n",
    "                dic[i] = 1\n",
    "        for j in nums:\n",
    "            if j + diff in dic and j + 2 * diff in dic:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        triplets = 0\n",
    "        for num in nums:\n",
    "            if num+diff in nums and num + diff *2 in nums:\n",
    "                triplets += 1\n",
    "\n",
    "        return triplets\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "\n",
    "        res = 0\n",
    "        n = len(nums)\n",
    "\n",
    "        unorder = nums\n",
    "        for v in nums:\n",
    "            if v - diff in unorder and v + diff in unorder:\n",
    "                res += 1\n",
    "\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        return sum(x + diff in nums and x + diff * 2 in nums for x in nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        cnt=0\n",
    "        for i,j,k in combinations(nums,3):\n",
    "            if j-i==k-j==diff:\n",
    "                cnt+=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i + 1,len(nums)):\n",
    "                for k in range(j + 1,len(nums)):\n",
    "                    if nums[j] - nums[i] == diff and nums[k] - nums[j] == diff:\n",
    "                        count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        cnt = 0\n",
    "        for i in nums:\n",
    "            if (i + diff) in nums and (i + 2 * diff) in nums:\n",
    "                cnt += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        res = 0\n",
    "        s = set()\n",
    "        for i in nums:\n",
    "            if i - diff in s and i - 2*diff in s:\n",
    "                res += 1\n",
    "            s.add(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        dic = {}\n",
    "        ans = 0\n",
    "        for i in nums:\n",
    "            if i not in dic:\n",
    "                dic[i] = 1\n",
    "        for j in nums:\n",
    "            if j + diff in dic and j + 2 * diff in dic:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        n = len(nums)\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            target = nums[i] + diff\n",
    "            for j in range(i+1, n):\n",
    "                if nums[j] > target:\n",
    "                    break\n",
    "                if nums[j] == target:\n",
    "                    target2 = target + diff\n",
    "                    for a in range(j+1, n):\n",
    "                        if nums[a] > target2:\n",
    "                            break\n",
    "                        if nums[a] == target2:\n",
    "                            res += 1\n",
    "                            break\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        #s=set(nums)\n",
    "        #ans=0\n",
    "        #for x in nums:\n",
    "        #    if x-diff in s and x+diff in s:\n",
    "        #        ans += 1\n",
    "        #return ans\n",
    "\n",
    "        i=0\n",
    "        j=1\n",
    "        ans=0\n",
    "        for x in nums[2:]:\n",
    "            while nums[j]+diff<x:\n",
    "                j += 1\n",
    "            if nums[j]+diff>x:\n",
    "                continue\n",
    "            while nums[i]+diff<nums[j]:\n",
    "                i += 1\n",
    "            if nums[i]+diff==nums[j]:\n",
    "                ans += 1\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i+1,len(nums)):\n",
    "                for k in range(j+1,len(nums)):\n",
    "                    if nums[j]-nums[i]==diff and nums[k]-nums[j]==diff:\n",
    "                        ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "      nums_set = set(nums)\n",
    "      answer = 0\n",
    "      for num in nums:\n",
    "        if (num + diff) in nums_set and (num + diff * 2) in nums_set:\n",
    "          answer += 1\n",
    "      return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] + diff in nums and nums[i] + 2 * diff in nums:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        n = len(nums)\n",
    "        res=0\n",
    "        for i in range(n-2):\n",
    "            if nums[i]+diff in nums and nums[i] +2*diff in nums:\n",
    "                res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                if nums[j] - nums[i] != diff:\n",
    "                    continue\n",
    "                for k in range(j + 1, n):\n",
    "                    if nums[k] - nums[j] == diff:\n",
    "                        ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        nums=set(nums)\n",
    "        return sum(x-diff in nums and x+diff in nums for x in nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        res = 0\n",
    "        for i in nums:\n",
    "            num_1 = i + diff\n",
    "            num_2 = i + diff * 2\n",
    "            if num_1 in nums and num_2 in nums:\n",
    "                res += 1\n",
    "            if num_2 >nums[len(nums)-1]:\n",
    "                return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        n=len(nums)\n",
    "        a=0\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                for k in range (j+1,n):\n",
    "                    if nums[j]-nums[i]==diff and nums[k]-nums[j]==diff:\n",
    "                        a+=1\n",
    "                    else:\n",
    "                        continue\n",
    "        return a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        res=0\n",
    "        for i in nums:\n",
    "            mid=[]\n",
    "            if i+diff in nums and i+2*diff in nums:\n",
    "                res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        i = j = k = 0\n",
    "        res = 0\n",
    "        while i < len(nums) - 2:\n",
    "            while j < len(nums) - 1:\n",
    "                if nums[j] - nums[i] == diff:\n",
    "                    while k < len(nums):\n",
    "                        if nums[k] - nums[j] == diff:\n",
    "                            res += 1\n",
    "                        k += 1\n",
    "                k = 0\n",
    "                j += 1\n",
    "            j = 0\n",
    "            i += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        \"\"\"\n",
    "        1.数组内不存在重复的数字(题目给定严格单调) set\n",
    "        \"\"\"\n",
    "        ans = 0\n",
    "        nums = set(nums)\n",
    "        for j in nums:\n",
    "            if j - diff in nums and diff + j in nums:\n",
    "                ans =  ans + 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        return sum(1 for x in nums if (x+diff in nums and x + 2*diff in nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        ans = 0\n",
    "        for num in nums:\n",
    "            if (num + diff) in nums and (num + 2 * diff) in nums:\n",
    "                ans +=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        ans = 0\n",
    "        dic = Counter(nums)\n",
    "        for i in range(len(nums) - 2):\n",
    "            if nums[i] + diff in dic and nums[i] + 2 * diff in dic:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(nums)-2):\n",
    "            mid = nums[i+1]\n",
    "            for low in nums[:i+1]:\n",
    "                if mid - low == diff:\n",
    "                    for high in nums[i+2:]:\n",
    "                        if high - mid == diff:\n",
    "                            count += 1\n",
    "        \n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        ans = 0\n",
    "        for num in nums:\n",
    "            diff_num = num + diff\n",
    "            if diff_num in nums:\n",
    "                diff_num2 = diff_num + diff\n",
    "                if diff_num2 in nums:\n",
    "                    ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        count=0\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(len(nums)):\n",
    "                for k in range(len(nums)):\n",
    "                    if i<j<k and nums[j]-nums[i]==diff and nums[k]-nums[j]==diff:\n",
    "                        count+=1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i+1,len(nums),1):\n",
    "                if nums[j] - nums[i] == diff:\n",
    "                    for k in range(j+1,len(nums),1):\n",
    "                        if nums[k] - nums[j] == diff:\n",
    "                            ans += 1\n",
    "                            break\n",
    "                        elif nums[k] - nums[j] > diff:\n",
    "                            break\n",
    "                elif nums[j] - nums[i] > diff:\n",
    "                    break\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        ans=0\n",
    "        for i in nums:\n",
    "            if i+diff in nums and i+2*diff in nums:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        Num_set = set(nums)\n",
    "        count=0\n",
    "        for i in nums:\n",
    "            if i+diff in Num_set and i-diff in Num_set:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        return sum(1 for x in nums if (x+diff in nums and x + 2*diff in nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        dicts = defaultdict(int)\n",
    "        for x in nums:\n",
    "            check = False\n",
    "            for key in dicts.keys():\n",
    "                if (x - key) % diff == 0:\n",
    "                    if (x - key) == diff * dicts[key]:\n",
    "                        check = True\n",
    "                        dicts[key] += 1\n",
    "            if not check:\n",
    "                dicts[x] = 1\n",
    "        \n",
    "        res = 0\n",
    "        for value in dicts.values():\n",
    "            if value >= 3:\n",
    "                res += value - 2\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        # res = 0\n",
    "        # for i in range(len(nums)):\n",
    "        #     for j in range(i+1, len(nums)):\n",
    "        #         for k in range(j+1, len(nums)):\n",
    "        #             if nums[j] - nums[i] == diff and nums[k] - nums[j] == diff:\n",
    "        #                 res += 1\n",
    "        # return res\n",
    "        res = 0\n",
    "        for i in nums:\n",
    "            if i - diff in nums and i + diff in nums:\n",
    "                res += 1\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        #dict_n = {}\n",
    "        cnt = 0\n",
    "        for item in nums:\n",
    "            if item+diff in nums and item+2*diff in nums:\n",
    "                cnt += 1\n",
    "        \n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(nums)):\n",
    "            three = 1\n",
    "            for j in range(i+1,len(nums)):\n",
    "                if nums[j] == nums[i] + diff:\n",
    "                    three += 1\n",
    "                if nums[j] == nums[i] + 2 * diff:\n",
    "                    three += 1\n",
    "            if three == 3:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        i = 0\n",
    "        ans = 0\n",
    "        n = len(nums) - 2\n",
    "        while i < n:\n",
    "            if nums[i]+diff in nums and nums[i] + 2*diff in nums:\n",
    "                ans += 1\n",
    "            i += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        a=0\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(len(nums)):\n",
    "                for k in range(len(nums)):\n",
    "                    if nums[k]-nums[j]==diff and k!=j:\n",
    "                        if nums[j]-nums[i]==diff and j!=i:\n",
    "                            a+=1\n",
    "        return a                          \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        count = 0\n",
    "        for a, i in enumerate(nums):\n",
    "            t = [i]\n",
    "            for j in nums[a + 1:]:\n",
    "                if abs(i - j) == diff:\n",
    "                    t.append(j)\n",
    "                if len(t) == 2 and abs(t[1] - j) == diff:\n",
    "                    count += 1\n",
    "                    continue\n",
    "            if len(t) == 3:\n",
    "                count += 1\n",
    "\n",
    "        return count    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        n=len(nums)\n",
    "        ans=0\n",
    "        s=set()\n",
    "        for i in range(n):\n",
    "            if nums[i]-diff in s and nums[i]-2*diff in s:\n",
    "                ans+=1\n",
    "            s.add(nums[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        ans, n = 0, len(nums)\n",
    "        for i in range(n - 2):\n",
    "            for j in range(i + 1, n - 1):\n",
    "                if nums[j] - nums[i] == diff:\n",
    "                    for k in range(j + 1, n):\n",
    "                        if nums[k] - nums[j] == diff:\n",
    "                            ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        return sum(b - a == diff and c - b == diff for a, b, c in combinations(nums, 3))\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        s = set(nums)\n",
    "        return sum(x - diff in s and x + diff in s for x in s)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        n = len(nums)\n",
    "        count = 0\n",
    "        for i in range(n):\n",
    "            if nums[i] - diff in nums and nums[i] + diff in nums:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(1, len(nums)-1):\n",
    "            if nums[i] - diff in nums and nums[i] + diff in nums:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        return sum(1 for x in nums if x+diff in nums and x + 2*diff in nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        ans, s = 0, set()\n",
    "        for x in nums:\n",
    "            if x - diff in s and x - diff * 2 in s:\n",
    "                ans += 1\n",
    "            s.add(x)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        return sum(b - a == diff and c - b == diff for a, b, c in combinations(nums, 3))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        n=len(nums)\n",
    "        r =0 \n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                for k in range(j+1,n):\n",
    "                    if nums[j]-nums[i]==diff and nums[k]-nums[j]==diff:\n",
    "                        r+=1\n",
    "        return r "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        ans, i, j = 0, 0, 1\n",
    "        for x in nums:  # x = nums[k]\n",
    "            while nums[j] + diff < x:\n",
    "                j += 1\n",
    "            if nums[j] + diff > x:\n",
    "                continue\n",
    "            while nums[i] + diff * 2 < x:\n",
    "                i += 1\n",
    "            if nums[i] + diff * 2 == x:\n",
    "                ans += 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        ans = 0\n",
    "        for na,a in enumerate(nums):\n",
    "            for nb,b in enumerate(nums[na+1:]):\n",
    "                if b-a == diff:\n",
    "                    for nc,c in enumerate(nums[nb+1:]):\n",
    "                        if c-b == diff:\n",
    "                            ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        d=[]\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i+1,len(nums)):\n",
    "                if nums[j]-nums[i]==diff:\n",
    "                    d.append([i,j])\n",
    "        \n",
    "        s=0\n",
    "\n",
    "        for i in range(len(d)):\n",
    "            t=d[i][1]\n",
    "            for j in range(len(d)):\n",
    "                if t==d[j][0]:\n",
    "                    s+=1\n",
    "                    break\n",
    "        return s \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        a=0\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i+1,len(nums)):\n",
    "                for k in range(j+1,len(nums)):\n",
    "                    if nums[j]-nums[i]==diff and nums[k]-nums[j]==diff:\n",
    "                        a+=1\n",
    "        return a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:\n",
    "        n = len(nums)\n",
    "\n",
    "        res = 0\n",
    "        for i in range(n - 2):\n",
    "            for j in range(i + 1, n - 1):\n",
    "                for k in range(j + 1, n):\n",
    "                    if nums[k] - nums[j] == diff and nums[j] - nums[i] == diff:\n",
    "                        res += 1\n",
    "\n",
    "        return res"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
